/**
 *    Copyright (C) 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_MODELS_BLOB_MAP_MODEL_API_HPP
#define _CUBEFS_CORE_MODELS_BLOB_MAP_MODEL_API_HPP

#include <cubefs/core/models/blob_map/names.hpp>
#include <cubefs/api/models/logs.hpp>

#include <cubefs/core/tools/bitmap.hpp>

#define CUBEFS_SOURCE "cubefs/core/models/blob_map/model/api.hpp:" CUBEFS_AT

namespace cubefs    {
namespace ctr       {

CUBEFS_MODEL_PART_BEGIN(cubefs::models::blob_map::ApiName)

    typedef typename Base::Types                                                Types;
    typedef typename Base::PageMgr                                              PageMgr;

    typedef typename Base::Page                                                 Page;
    typedef typename Base::ID                                                   ID;


    typedef typename Base::NodeBase                                             NodeBase;
    typedef typename Base::Counters                                             Counters;
    typedef typename Base::Iterator                                             Iterator;

    typedef typename Base::NodeDispatcher                                       NodeDispatcher;
    typedef typename Base::RootDispatcher                                       RootDispatcher;
    typedef typename Base::LeafDispatcher                                       LeafDispatcher;
    typedef typename Base::NonLeafDispatcher                                    NonLeafDispatcher;

    typedef typename Base::Node2RootMap                                         Node2RootMap;
    typedef typename Base::Root2NodeMap                                         Root2NodeMap;

    typedef typename Base::NodeFactory                                          NodeFactory;

    typedef typename Base::Key                                                  Key;
    typedef typename Base::Value                                                Value;

    typedef typename Base::ApiKeyType                                           ApiKeyType;
    typedef typename Base::ApiValueType                                         ApiValueType;

    typedef typename Types::DataPageType                                        DataPageType;
    typedef typename Types::BufferType                                          Buffer;
    typedef typename Types::BufferContentDescriptorType                         BufferContentDescriptor;
    typedef typename Types::CountDataType                                       CountData;

    static const Int Indexes                                                    = Base::Indexes;

    

    void copy_data(const Buffer& data, DataPageType* page, BigInt start, BigInt pos, BigInt length) {
        CUBEFS_TRACE(me_, "copy_data", data.GetByteSize(), page->id(), start, pos, length);

        cubefs::tools::CopyBuffer(data.Addr(start), &page->data().value(pos), length);
    }

    void move_data(DataPageType *from, DataPageType *to, BigInt local_idx, CountData &prefix, BigInt* keys) {
        CUBEFS_TRACE(me_, "move_data", from->id(), to->id(), local_idx);

        BigInt length = from->data().size() - local_idx;

        keys[0] = -length;

        cubefs::tools::CopyBuffer(&to->data().value(0), &to->data().value(length), to->data().size());

        cubefs::tools::CopyBuffer(&from->data().value(local_idx), &to->data().value(0), length);
    }
    
    


CUBEFS_MODEL_PART_END

}
}

#undef CUBEFS_SOURCE

#endif
